<?php
namespace app\client\controller\message;
use think\db\Expression;

class SendmsgController extends \think\Controller {

    /**
     *  @brief  : 短信验证
     *  @return : json
     *  @details:
     *
     */
    public function validAction()
    {

        trace($this->request->param());
        $phonecode = $this->request->param();  //手机号
        $checkcode = $this->request->param('checkcode/d', 0);  //验证码
        $msg = '【牵手互娱】您的验证码是：'.$checkcode.',请在有效期内进行验证！';
        $cond['Status'] = 1;
        $cond['BalanceRMB'] = ['gt',0.06];
        $users = table('SMSConfig','rydb.websms')->where($cond)->find();
        if (empty($users)){
            return "短信配置错误，暂停使用";
        }
        $data = array (
            'src' => $users['Users'], // 你的用户名, 必须有值
            'pwd' => $users['Pwd'], // 你的密码, 必须有值
            'ServiceID' => 'SEND', //固定，不需要改变
            'dest' => '86'.$phonecode['telp'], // 你的目的号码【收短信的电话号码】, 必须有值
            'sender' => '', // 你的原号码,可空【大部分国家原号码带不过去，只有少数国家支持透传，所有一般为空】
            'codec' => '8', // 编码方式， 与msg中encodeHexStr 对应
            // codec=8 Unicode 编码,  3 ISO-8859-1, 0 ASCII
            'msg' => $this->encodeHexStr(8,$msg) // 编码短信内容
        );

        $uri = "http://210.51.190.233:8085/mt/mt3.ashx"; // 接口地址
        $ch = curl_init();
        trace($ch);
        curl_setopt ( $ch, CURLOPT_URL, $uri );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
        $return = curl_exec ( $ch ); //$return  返回结果，如果是以 “-” 开头的为发送失败，请查看错误代码，否则为MSGID
        curl_close ( $ch );
        $usr = $this->checkRMB($users['Users'],$users['Pwd'],$users['ID']);
        return $return;
        trace($return);
        print_r($return);

    }
    protected  function checkRMB($users,$pwd,$id){
        $url = 'http://m.isms360.com:8085/querybalance.ashx';
        $data = [
            'User'=>$users,
            'Pwd' =>$pwd
        ];
        $ch = curl_init();
        trace($ch);
        curl_setopt ( $ch, CURLOPT_URL, $url);
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
        $return = curl_exec ( $ch ); //$return  返回结果，如果是以 “-” 开头的为发送失败，请查看错误代码，否则为MSGID
        curl_close ( $ch );
        $res = table('SMSConfig','rydb.websms')->where('ID',$id)->update(['BalanceRMB'=>$return]);
        return $res;
        trace($return);
    }

    protected function encodeHexStr($dataCoding, $realStr) {

        if ($dataCoding == 15)
        {
            return strtoupper(bin2hex(iconv('UTF-8', 'GBK', $realStr)));
        }
        else if ($dataCoding == 3)
        {
            return strtoupper(bin2hex(iconv('UTF-8', 'ISO-8859-1', $realStr)));
        }
        else if ($dataCoding == 8)
        {
            return strtoupper(bin2hex(iconv('UTF-8', 'UCS-2BE', $realStr)));
        }
        else
        {
            return strtoupper(bin2hex(iconv('UTF-8', 'ASCII', $realStr)));
        }
    }
}

?>